<?php

namespace App\Http\Controllers;

use App\Http\Requests\LiveDefaultImageRequest;
use App\Models\Activity;
use App\Models\LiveDefaultImage;
use App\Models\LiveUser;
use App\Models\User;
use Illuminate\Http\Request;
use \DB;
use App\Http\Controllers\Controller;
use App\Models\Live;

class LiveDefaultImageController extends CommonController
{
    /**
     * 获取列表
     */
    public function getIndex(Request $request)
    {
        $data = $request->input();
        $lists = LiveDefaultImage::where('live_id', $data['live_id'])->orderBy('id', 'desc')->paginate(10);

        return view('livedefaultimage.index')->with('lists', $lists)->with('actions', $this->actions)->with('live_id', $data['live_id']);
    }

    public function getCreate(Request $request)
    {
        $data = $request->input();

        return view('livedefaultimage.create')->with('live_id', $data['live_id']);
    }

    public function postCreate(LiveDefaultImageRequest $request)
    {
        $data = $request->input();
        LiveDefaultImage::create(
            [
                'name'    => $data['at_name'],
                'type'    => $data['at_filetype'],
                'url'     => $data['at'],
                'live_id' => $data['live_id'],
                'sort'    => $data['sort']
            ]
        );

        return AjaxCallbackMessage('创建成功', true, url('livedefaultimage/index?live_id='.$data['live_id']));
    }

    public function getEdit(Request $request)
    {
        $data = $request->input();
        $list = LiveDefaultImage::where('id', $data['id'])->first();

        return view('livedefaultimage.edit')->with('list', $list);
    }

    public function postEdit(LiveDefaultImageRequest $request)
    {
        $data = $request->input();

        LiveDefaultImage::where('id', $data['id'])
            ->update(
                [
                    'name'    => $data['at_name'],
                    'type'    => $data['at_filetype'],
                    'url'     => $data['at'],
                    'live_id' => $data['live_id'],
                    'sort'    => $data['sort']
                ]
            );

        return AjaxCallbackMessage('编辑成功', true, url('livedefaultimage/index?live_id='.$data['live_id']));
    }



    public function postDelete(Request $request)
    {
        $data = $request->input();
        $image = LiveDefaultImage::where('id', $data['atid'])->first();
        if(is_object($image)){
            $uploadDir = config('upload.uploadDir');
            @unlink($uploadDir.substr($image->url,8));
        }

        LiveDefaultImage::where('id', $data['atid'])->delete();

        return AjaxCallbackMessage('删除成功', true, url('livedefaultimage/index?live_id='.$image->live_id));


    }
}
